package com.ck.common;

import com.ck.core.util.DateUtils;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
* @Description: 用户会话信息（仅保存关键信息，业务需要的其他用户信息需要自行查询获得）
 *             //  see com.ck.ips.model.User
* @Author: hf
* @Date:    2019/4/29 9:51
* @Version: 1.0
*/
@JsonIgnoreProperties
public class LoginFailRecord {
	/** 允许登录失败的次数，超过该次数会冻结帐号登录功能 */
	public static final int MAX_FAIL_LOGIN_COUNT = 5;
	/** 登录冻结时间（毫秒） */
	public static final long LOGIN_FREEZE_TIME = 6 * 60 * 60 * 1000L;

	private String userName;
	private int failCount;
	private Long freezeTime;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getFailCount() {
		return failCount;
	}

	public void setFailCount(int failCount) {
		this.failCount = failCount;
	}

	public Long getFreezeTime() {
		return freezeTime;
	}

	public void setFreezeTime(Long freezeTime) {
		this.freezeTime = freezeTime;
	}

	public String remainingTime() {
		return DateUtils.getTimeStampDiff(System.currentTimeMillis(),freezeTime);
	}

	public void log() {
		this.failCount = failCount + 1;
		if ( this.failCount >= MAX_FAIL_LOGIN_COUNT ) {
			freeze();
		}
	}

	public LoginFailRecord() {
		super();
	}

	public LoginFailRecord(String userName) {
		this.userName = userName;
	}

	/** 是否可以继续登录 */
	public boolean canLogin(){
		return this.failCount <= MAX_FAIL_LOGIN_COUNT && this.freezeTime == null;
	}

	/** 设置登录冻结时间 */
	public void freeze() {
		this.freezeTime = System.currentTimeMillis() + LOGIN_FREEZE_TIME;
	}

	/** 判断是否需要解冻 */
	public boolean needUnFreeze() {
		return this.freezeTime != null && this.freezeTime <= System.currentTimeMillis();
	}

}